package com.game.frame.GameFrame.GameFrameMain;

import com.game.utils.StaticValue;
import java.awt.*;
import java.util.Random;

/**
 * Beast 类代表游戏中的野兽角色
 */
public class Beast extends BaseCharacter {
    // 随机数生成器，用于决定移动方向和状态切换
    private final Random random = new Random();
    // 移动方向，0表示左，1表示右
    private int moveDirection = 1;
    // 计数器，用于跟踪移动持续时间
    private int movementCounter = 0;
    // 移动持续时间常量
    private static final int MOVEMENT_DURATION = 100;
    // 移动速度常量
    private static final int MOVE_SPEED = 2;
    // 检测范围
    private static final int DETECT_RANGE = 150;
    // 攻击冷却相关
    private int attackCoolingDown = 0;
    // 攻击冷却时间
    private static final int ATTACK_COOLING_DOWN_TIME = 50;

    // 定义不同状态的帧范围
    private static final int MOVE_FRAMES_START = 0;
    private static final int MOVE_FRAMES_END = 5;
    private static final int SKILL_FRAMES_START = 6;
    private static final int SKILL_FRAMES_END = 13;
    private static final int STAND_FRAMES_START = 14;
    private static final int STAND_FRAMES_END = 19;

    // 当前动画状态
    private int currentState = 0; // 0:站立 1:移动 2:技能

    // 血条相关常量
    private static final int BLOOD_BAR_WIDTH = 50;  // 血条宽度
    private static final int BLOOD_BAR_HEIGHT = 3;  // 血条高度
    private static final int BLOOD_BAR_Y_OFFSET = 1; // 血条距离野兽顶部的距离

    /**
     * 构造函数，初始化野兽角色
     * @param panel 游戏面板对象，用于获取面板宽度和高度
     */
    public Beast(GamePanel panel) {
        super(panel,
                new Random().nextInt(panel.getPanelWidth() - CHARACTER_WIDTH),
                350, // 设置固定的Y轴高度，与英雄相同
                100); // 初始生命值为100
        this.attackDamage = 1;
        this.attackRange = 5;
        this.currentImage = StaticValue.rightEnemyImgs.get(STAND_FRAMES_START);
    }

    /**
     * 监测英雄是否在攻击范围内，并设置朝向
     * @return true表示英雄在攻击范围内，否则返回false
     */
    private boolean isHeroInRange() {
        for (Hero hero : gamePanel.getCharacters()){
            int distanceX = Math.abs((hero.getX() + CHARACTER_WIDTH / 2) - (x + CHARACTER_WIDTH / 2));
            int distanceY = Math.abs((hero.getY() + CHARACTER_HEIGHT / 2) - (y + CHARACTER_HEIGHT / 2));
            if (distanceX <= DETECT_RANGE && distanceY <= CHARACTER_HEIGHT / 2) {
                // 设置朝向
                facingRight = hero.getX() > x;
                return true;
            }
        }
        return false;
    }

    /**
     * 更新动画状态和位置
     */
    @Override
    public void updateAnimation() {
        // 如果死亡 不做任何处理直接返回
        if (isDead()){
            return;
        }

        // 更新技能冷却
        if (attackCoolingDown > 0){
            attackCoolingDown--;
        }

        // 检查是否有英雄在范围内 并且技能冷却完毕
        if (isHeroInRange() && currentState == 0){
            // 进入攻击状态
            currentState = 2;
            currentFrameIndex = SKILL_FRAMES_START;
            attackCoolingDown = ATTACK_COOLING_DOWN_TIME;      // 设置技能冷却时间
        }

        // 更新移动方向
        movementCounter++;
        if (movementCounter >= MOVEMENT_DURATION) {
            movementCounter = 0;
            moveDirection = random.nextInt(2); // 只在左右方向移动：0:左, 1:右
            currentState = random.nextInt(3); // 随机切换状态
        }

        // 根据方向移动
        if (currentState == 1) { // 移动状态
            switch (moveDirection) {
                case 0: // 左
                    move(-MOVE_SPEED, 0);
                    facingRight = false;
                    break;
                case 1: // 右
                    move(MOVE_SPEED, 0);
                    facingRight = true;
                    break;
            }
        }


        // 更新动画帧
        currentFrameIndex++;
        switch (currentState) {
            case 0: // 站立
                if (currentFrameIndex > STAND_FRAMES_END || currentFrameIndex < STAND_FRAMES_START) {
                    currentFrameIndex = STAND_FRAMES_START;
                }
                break;
            case 1: // 移动
                if (currentFrameIndex > MOVE_FRAMES_END || currentFrameIndex < MOVE_FRAMES_START) {
                    currentFrameIndex = MOVE_FRAMES_START;
                }
                break;
            case 2: // 技能
                if (currentFrameIndex > SKILL_FRAMES_END || currentFrameIndex < SKILL_FRAMES_START) {
                    currentFrameIndex = SKILL_FRAMES_START;
                }
                if (currentFrameIndex == SKILL_FRAMES_END) {
                    currentState = 0; // 技能动画结束后回到站立状态
                }
                break;
        }

        // 根据朝向选择对应的图片
        currentImage = facingRight ?
                StaticValue.rightEnemyImgs.get(currentFrameIndex) :
                StaticValue.leftEnemyImgs.get(currentFrameIndex);

        if (currentState == 2){
            isAttacking = true;
            updateAttackBox();

            for (Hero hero : gamePanel.getCharacters()) {
                if (checkHit(hero)) {
                    hero.takeHit(attackDamage, x > hero.getX());
                }
            }
        } else {
            isAttacking = false;
        }
    }

    /**
     * 绘制野兽和血条
     * @param g Graphics对象，用于绘制
     */
    @Override
    public void drawBlood(Graphics g) {
        // 已经死亡就不做绘制
        if (isDead()){
            return;
        }

        // 绘制野兽
        g.drawImage(currentImage, x, y, CHARACTER_WIDTH, CHARACTER_HEIGHT, null);

        // 计算血条位置（居中显示）
        int bloodBarX = x + (CHARACTER_WIDTH - BLOOD_BAR_WIDTH) / 2;
        int bloodBarY = y + BLOOD_BAR_Y_OFFSET;

        // 绘制血条背景（灰色）
        g.setColor(Color.GRAY);
        g.fillRect(bloodBarX, bloodBarY, BLOOD_BAR_WIDTH, BLOOD_BAR_HEIGHT);

        // 绘制当前血量（红色）
        g.setColor(Color.RED);
        int currentBloodWidth = (int)((float)health / 100 * BLOOD_BAR_WIDTH);
        g.fillRect(bloodBarX, bloodBarY, currentBloodWidth, BLOOD_BAR_HEIGHT);
    }
}
